package com.polaris.live.popup

import android.content.Context
import android.view.View
import android.widget.SeekBar
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.polaris.live.R.layout
import com.polaris.live.adapter.BeautyAdapter
import com.polaris.live.adapter.BeautyTitleAdapter
import com.polaris.live.databinding.PopupBeautySetBinding
import com.polaris.live.popup.base.BaseLiveBottomPopupView
import com.polaris.live.resp.back_resp.BeautyItem
import com.polaris.live.utils.zego.beauty.BeautyFilterEnum
import com.polaris.live.utils.zego.beauty.FaceManager

/**
 * 美颜调整半弹窗
 *
 * @author Created by 半仙 on 2023/9/7/007 17:05
 */
class BeautyPopup(
    context: Context,
    private val onChangeBeauty: ((beautyItem: BeautyItem, type: Long) -> Unit)
) : BaseLiveBottomPopupView<PopupBeautySetBinding>(context, { PopupBeautySetBinding.bind(it) }) {

    private val mTitleAdapter by lazy { BeautyTitleAdapter() }
    private val mAdapter by lazy { BeautyAdapter() }

    private val mBeauty = FaceManager.getBeauty()
    private var mFaceFilter = FaceManager.getBeautyFilter()

    private var mSelectTitle = 0
    private var mSelectChild = 0

    override fun getImplLayoutId(): Int {
        return layout.popup_beauty_set
    }

    override fun onCreate() {
        super.onCreate()

        val beautify = mBeauty.item[0]

        mBinding.recycler.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            adapter = mAdapter
        }

        mBinding.titleRecycler.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            adapter = mTitleAdapter
        }

        mAdapter.setList(beautify.beautyItem)
        mTitleAdapter.setList(mBeauty.item)

        getCurrentItem()?.intensity?.let {
            mBinding.seek.progress = it
        }

        mBinding.seek.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                val item = getCurrentItem()

                item?.intensity = progress

                val tempType = getSelectItem()?.type
                if (item != null && tempType != null) {
                    onChangeBeauty.invoke(item, tempType)
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {

            }
        })

        mTitleAdapter.setOnItemClickListener { _, _, position ->
            val list = mBeauty.item.getOrNull(position)?.beautyItem
            mAdapter.setList(list)

            mTitleAdapter.clearSelected()
            mTitleAdapter.setSelect(position)

            if (!list.isNullOrEmpty()) {
                if (position != mSelectTitle && mSelectChild > list.size - 1) {
                    mSelectChild = 0
                }
            }

            mSelectTitle = position

            handleCurrentItem(true)

            mAdapter.setSelect(mSelectChild)
            mBinding.recycler.scrollToPosition(mSelectChild)
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            mAdapter.clearSelected()
            mAdapter.setSelect(position)

            mSelectChild = position

            handleCurrentItem(false)
        }
    }

    private fun handleCurrentItem(switchTitle: Boolean) {
        val titleItem = getSelectItem()

        if (switchTitle && titleItem != null) {
            val index = titleItem.beautyItem.indexOfFirst { it.type == mFaceFilter }
            if (index != -1) {
                mSelectChild = index
            }
        }

        val item = getCurrentItem()
        if (titleItem != null && item != null) {
            if (!switchTitle && mSelectTitle == 2) {
                mFaceFilter = item.type
            }

            mBinding.seek.progress = item.intensity
            if (mSelectTitle == 2 && item.type == BeautyFilterEnum.ORIGIN.type) {
                mBinding.seek.visibility = GONE
            } else {
                mBinding.seek.visibility = VISIBLE
            }
        } else {
            mBinding.seek.visibility = GONE
        }
    }

    private fun getSelectItem() = mBeauty.item.getOrNull(mSelectTitle)
    private fun getCurrentItem() = getSelectItem()?.beautyItem?.getOrNull(mSelectChild)

    override fun getCardView(): CardView = mBinding.flBg

    override fun getContentView(): View = mBinding.bottom

    override fun onDismiss() {
        super.onDismiss()

        FaceManager.setBeauty(mFaceFilter)
    }
}